package com.zy.cat.common.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.RandomUtils;

import com.zy.cat.entity.game.resp.Prize;

/**
 *  计算工具类
 * @author Admin
 *
 */
public class CalculateUtil {

	/**
	 * 获取返利金额
	 * @param payAmount 折扣价格
	 * @param couponAmount 优惠券金额
	 * @param commissionRate 佣金比例
	 * @return
	 */
	public static long getRebateAmount(double payAmount,double couponAmount,double commissionRate) {
		double amount=(payAmount-couponAmount)*(commissionRate/100.00);
//		if (amount>0&&amount<=3) {
//			return (long)(amount*0.35*100);
//		}else if (amount>3&&amount<=8) {
//			return (long)(amount*0.32*100);
//		}else {
//			return (long)(amount*0.28*100);
//		}
		return (long)(amount*0.40*100);
	}
	
	/**
	 * 获取返利金额
	 * @param actualPrice 劵后价
	 * @param commissionRate 佣金比例
	 * @return
	 */
	public static long getRebateAmount(double actualPrice,double commissionRate) {
		double amount=actualPrice*(commissionRate/100.00);
//		if (amount>0&&amount<=3) {
//			return (long)(amount*0.35*100);
//		}else if (amount>3&&amount<=8) {
//			return (long)(amount*0.32*100);
//		}else {
//			return (long)(amount*0.28*100);
//		}
		return (long)(amount*0.40*100);
	}
	
	/**
	 * 按比例随机抽取一项
	 * @param fieldArray 类型值数组
	 * @param proportions 与类型值对应 的占比值
	 * @return 类型值
	 */
	public static String ratioExtract(String[] fieldArray,double[] proportions) {
		//判断两个数组长度是否相等
		if(fieldArray.length!=proportions.length) {
			return "两数组长度不相等,无法执行";
		}
		
		//占比之和
		double sum=0.00;
		//分段数组(20,30,60)
		double[] subArray=new double[proportions.length+1];
		//将概率分段
		for (int i = 0; i < proportions.length; i++) {
			subArray[i]=sum;
			sum+=proportions[i];
		}
		//加上取最大的值
		subArray[subArray.length-1]=sum;
		
		/* 产生随机数 */
		double rand = RandomUtils.nextDouble(0, sum);
		
		//返回字符
		String field=null;
		for (int i = 0; i < subArray.length; i++) {
			if (rand>=subArray[i] && rand<subArray[i+1]) {
				field=fieldArray[i];
			}
		}
		return field;
	}
	
	/**
	 * 按比例随机抽取一项
	 * @param list 奖品列表
	 * @return 类型值
	 */
	public static String ratioExtract(List<Prize> list) {
		//非空判断
		if (list==null || list.size()<1) {
			return null;
		}
		//占比之和
		double sum=0.00;
		//分段数组(20,30,60)
		double[] subArray=new double[list.size()+1];
		//将概率分段
		for (int i = 0; i < list.size(); i++) {
			subArray[i]=sum;
			sum+=list.get(i).getPrizeWeight()*list.get(i).getPrizeCount();
		}
		//加上取最大的值
		subArray[subArray.length-1]=sum;
		
		/* 产生随机数 */
		double rand = RandomUtils.nextDouble(0, sum);
		
		//返回字符
		String field=null;
		for (int i = 0; i < subArray.length; i++) {
			if (i==subArray.length-1) {
				return field;
			}
			if (rand>=subArray[i] && rand<subArray[i+1]) {
				field=list.get(i).getPrizeName();
				break;
			}
		}
		return field;
	}
	
	/**
	 * 双重分段抽取,
	 * @param fieldArray 分段数组, 参数值用"-"组装(例: {"6-14","14-23","23-32","32-40"})
	 * @param proportions 每段出现的概率
	 * @return 返回按比例抽取后, 分段范围内的随机一个值
	 */
	public static Integer ratioExtractDouble(String[] fieldArray,double[] proportions) {
		String string = ratioExtract(fieldArray,proportions);
		String[] split = string.split("-");
		int result = RandomUtils.nextInt(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
		return result;
	}
	
	
	public static List<Prize> getPrizeList(String[] prizeName,int[] prizeCounts,double[] prizeWeights) {
		List<Prize> list=new ArrayList<>();
		for (int i = 0; i < prizeName.length; i++) {
			list.add(new Prize(prizeName[i], prizeCounts[i], prizeWeights[i]));
		}
		return list;
	}
	
    /**
     * 判断文件大小
     *
     * @param len
     *            文件长度
     * @param size
     *            限制大小
     * @param unit
     *            限制单位（B,K,M,G）
     * @return
     */
    public static boolean checkFileSize(Long len, int size, String unit) {
//        long len = file.length();
        double fileSize = 0;
        if ("B".equals(unit.toUpperCase())) {
            fileSize = (double) len;
        } else if ("K".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1024;
        } else if ("M".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1048576;
        } else if ("G".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1073741824;
        }
        if (fileSize > size) {
            return false;
        }
        return true;
    }
	
	public static void main(String[] args) {
//		String[] fieldArray= {"0","1","2"};
//		double[] proportions= {20,50,30};
//		List<String> list = Arrays.asList(fieldArray);
//		int[] countArray= {0,0,0};
//		int index;
//		for (int i = 0; i < 1000; i++) {
//			String string = proportionExtract(fieldArray,proportions);
//			index=list.indexOf(string);
//			countArray[index]++;
//			System.out.println("抽取到类型值为:"+string);	
//		}
//		System.out.println("获取对应总数:"+Arrays.toString(countArray));
		
		//刮刮卡
//		String[] fieldArray= {"1","2","3","4","5","6"};
//		double[] proportions= {0,35,10,50,5,0};
//		int[] countArray= new int[fieldArray.length];
//		int index;
//		for (int i = 0; i < 20; i++) {
//			String string = ratioExtract(fieldArray,proportions);
//			index=Arrays.binarySearch(fieldArray, string);
//			countArray[index]++;
//			System.out.println("抽取到类型值为:"+string);	
//		}
//		System.out.println("获取对应总数:"+Arrays.toString(countArray));
//		List<Prize> list=new ArrayList<Prize>();
//		list.add(new Prize("拆红包红包",1,25));
//		list.add(new Prize("现金豆",3,75));
//		
//		for (int i = 0; i < 4; i++) {
//			String extract = ratioExtract(list);
//			System.out.println("抽中："+extract);
//			if ("拆红包红包".equals(extract)) {
//				list.get(0).setPrizeCount(list.get(0).getPrizeCount()-1);
//			}else {
//				list.get(1).setPrizeCount(list.get(1).getPrizeCount()-1);
//			}
//		}
		
		/**
		 * 转盘抽奖抽取现金豆
		 */
		List<Prize> prizeList=new ArrayList<>();
		
		List<Prize> advertTypeList=new ArrayList<>();
		
		List<Prize> btnTypeList=new ArrayList<>();
		
		//奖品类型 0.现金豆、 1礼包(广告)
		String[] prizeTypeArr= {"0","1"};
		//奖品类型 0.现金豆（占51）, 1礼包(广告)（占49）
		double[] prizeProportions={51,49};
		
		//广告类型 : 101.展示广告红包,102.互动广告红包
		String[] advertTypeArr= {"101","102"};
		//广告类型 : 101.展示广告红包（占21）,102.互动广告红包(占26)
		double[] advertProportions={21,26};
		
		//按钮类型： 0.领取礼包、1.继续游戏
		String[] btnTypeArr= {"0","1"};
		double[] btnProportions={19,32};
		
		for (int i = 0; i < prizeTypeArr.length; i++) {
			prizeList.add(new Prize(prizeTypeArr[i], (int)prizeProportions[i], prizeProportions[i]));
		}
		
		for (int i = 0; i < advertTypeArr.length; i++) {
			advertTypeList.add(new Prize(advertTypeArr[i], (int)advertProportions[i], advertProportions[i]));
		}
		
		for (int i = 0; i < btnTypeArr.length; i++) {
			btnTypeList.add(new Prize(btnTypeArr[i], (int)btnProportions[i], btnProportions[i]));
		}
		
		
		String ratioExtract = CalculateUtil.ratioExtract(prizeList);
		
		System.out.println(ratioExtract);
	}
	
}
